//
//  LGTabBar.swift
//  LGWeibo
//
//  Created by 李更 on 8/9/16.
//  Copyright © 2016 itcast. All rights reserved.
//

import UIKit
//声明协议
protocol LGTabBarDelegate : NSObjectProtocol {
    //声明协议方法
    func tabBarButtonClick()
}


class LGTabBar: UITabBar {
    
    //声明一个代理
    weak var LGDelegate : LGTabBarDelegate?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        addSubview(composeBtn)
        
        //遍历子控件
        
        
    }
    //懒加载添加字控件
    
    lazy var composeBtn :UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
     
        //设置背景图片
       
        button.setBackgroundImage(UIImage(named : "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
           button.setBackgroundImage(UIImage(named : "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置图片
        button.setImage(UIImage(named : "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named : "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
            return button
        
    }()
    
    //点击事件
    func btnClick(){
        LGDelegate?.tabBarButtonClick()
        
    }
    
    
    
    
    //给子控制器设置frame
    override func layoutSubviews() {
        
        
        let childW = self.frame.width * 0.2
        var index : CGFloat = 0
        
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.size.width = childW
                value.frame.origin.x = index * childW
                index++
                if index == 2 {
                    index++
                }
                
            }
            
        }

        composeBtn.center.x = self.center.x
        composeBtn.center.y = self.frame.height * 0.5
        
        
    }
    
    
    
}
